<!DOCTYPE html>
<html>
<body>
<p>setTimeOut order in Web Workers.</p>
<div id="result"></div>
<script type="text/javascript">
function log(message)
{
    document.getElementById("result").innerHTML += message + "</br>";
}

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var worker = new Worker('resources/worker-timeout-order.js');
// Start the first test of settimeout's
var currentTest = "TimeOutSuite1";
var lastTestResult = 0;
var testCounterLimit = 3;
var timerLimit = 3;
worker.postMessage("TS1");
worker.onmessage = function(evt) {
    if(lastTestResult == evt.data - 1) {
        lastTestResult = evt.data;
        // we got all the results in order
        if(lastTestResult == testCounterLimit) {
            if(currentTest == "TimeOutSuite1") {
                if(--timerLimit == 0) {
                    log("PASS : TimeOutSuite1"); 
                    worker.postMessage("TS2");
                    currentTest = "TimeOutSuite2";
                }
            }
            else if (currentTest == "TimeOutSuite2") {
                log("PASS : TimeOutSuite2"); 
            }            
            lastTestResult = 0;
        }
    }
    else {
        if(currentTest == "TS1") {
            if(--timerLimit == 0) {
                log("FAIL : TimeOutSuite1"); 
                worker.postMessage("TS2");
            }
        }
        else if (currentTest == "TS2") {
            log("FAIL : TimeOutSuite2"); 
        }        
        lastTestResult = 0;
    }
    if (evt.data == "DONE") {
        if (window.testRunner)
            testRunner.notifyDone();
    } 
}
</script>    
</body>
</html>